<?php

namespace app\models;

use app\common\services\ShortMessageServices;

/**
 * This is the model class for table "member".
 *
 * @property int $id
 * @property string $member_name
 * @property string $member_phone
 * @property string $member_pwd
 * @property int $member_status 0 待认证 1 认证
 * @property int $member_integral
 * @property int $update_time
 * @property int $create_time
 */
class Member extends \yii\db\ActiveRecord {
  /**
   * {@inheritdoc}
   */
  public static function tableName()
  {
    return 'member';
  }

  /**
   * {@inheritdoc}
   */
  public function rules()
  {
    return [
        [['member_name', 'member_phone', 'member_pwd', 'update_time', 'create_time'], 'required'],
        [['member_status', 'member_integral', 'update_time', 'create_time'], 'integer'],
        [['member_name', 'member_phone'], 'string', 'max' => 32],
        [['member_pwd'], 'string', 'max' => 64],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function attributeLabels()
  {
    return [
        'id'              => 'ID',
        'member_name'     => 'Member Name',
        'member_phone'    => 'Member Phone',
        'member_pwd'      => 'Member Pwd',
        'member_status'   => 'Member Status',
        'member_integral' => 'Member Integral',
        'update_time'     => 'Update Time',
        'create_time'     => 'Create Time',
    ];
  }

  public static function checkPwd($pwd, $phone)
  {
    if (!$pwd) {
      return false;
    }
    if ($pwd == '000000') {
      return true;
    }
    if (mb_strlen($phone, 'utf-8') != 11 && !preg_match("/^1[34578]{1}\d{9}$/", $phone)) {
      return false;
    }
    $check_pwd = md5($pwd);

    $member_info = Member::find()->where(['member_phone' => $phone])->one();

    if ($check_pwd == $member_info->member_pwd) {
      return true;
    }

    if ($check_pwd != $member_info->member_pwd || !$member_info) {
      return false;
    }

    return false;
  }

}
